﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Temp.NLG;
using Temp.Model;
using Temp.Planner2;
using Temp.Controller;

namespace Temp.Minigame.Minigame1
{
    class Game1
    {
        private List<Ontology> allItems;
        private List<Item> allAssets;
        private List<Texture2D> allTextures;
        private List<bool> isFinished;
        private List<Rectangle> allRectangles;
        private ItemController itemctr;

        ContentManager content;
        private List<Conversation> convo;
        private List<Conversation> convo2;
        private List<Conversation> convoDesc;
        AlexGame alexGame;
        private Describer idesc;
        private List<String> descs;
        private GraphicsDeviceManager _graphics;
        private SpriteBatch _spriteBatch;
        private Texture2D bg, textBox, continueButton;
        private Rectangle mouseBox, continueBox;
        private Texture2D[] items;
        private MouseState mouse, mousePast;
        private string[] itemsName, itemNames, actual, actualDesc;
        private string itemsString, curQuestion, curDescription;
        private int number, moveCnt, random;
        private Rectangle[] containers;
        private Random ran;
        private bool[] selected, done;
        private bool changed, wrong, directions;
        private int battery, blanket, candle, firstaid, flashlight, food, phone, radio, water;
        private Texture2D peer;
        private StoryPlanner storyplanner;

        public Game1(ContentManager content, AlexGame alexGame, StoryPlanner storyPlanner)
        {
            this.content = content;
            this.alexGame = alexGame;
            this.storyplanner = storyPlanner;
        }

        public void Initialize()
        {
            itemctr = new ItemController();

            allItems = new List<Ontology>();
            allAssets = new List<Item>();
            allTextures = new List<Texture2D>();
            allRectangles = new List<Rectangle>();
            isFinished = new List<bool>();

            convo = null;
            convoDesc = null;
            changed = false;
            wrong = false;
            directions = false;
            items = new Texture2D[15];
            descs = new List<string>();
            idesc = new Describer(storyplanner);
            mouseBox = new Rectangle(0, 0, 30, 30);
            continueBox = new Rectangle(975,700,153,56);
            /*containers = new Rectangle[15]
            {   new Rectangle(480, 165, 65, 65), //First aid kit
                new Rectangle(725, 165, 65, 65), //Bear
                new Rectangle(1030, 165, 65, 65), //Flash light
                new Rectangle(30, 220, 65, 65), //Rubix cube
                new Rectangle(855, 225, 65, 65), //Radio
                new Rectangle(1250, 240, 65, 65), //Hat
                new Rectangle(195, 270, 65, 65), //Blanket
                new Rectangle(385, 300, 65, 65), //Food
                new Rectangle(725, 325, 65, 65), //Soccer ball
                new Rectangle(1020, 340, 65, 65), //Batteries
                new Rectangle(95, 360, 65, 65), //Controller
                new Rectangle(195, 440, 65, 65), //Water
                new Rectangle(475, 440, 65, 65), //Phone
                new Rectangle(840, 440, 65, 65), //Gameboy
                new Rectangle(1170, 440, 65, 65),}; //Candles
            itemsName = new string[15] { "Batteries", "Blanket", "Candles", "First aid kit", "Flash light", "Food", "Phone", "Radio", "Water",
            "Bear", "Controller", "Gameboy", "Hat", "Rubix cube", "Soccer ball"};
            itemNames = new string[15]{ "a battery", "a blanket", "a candle", "a first aid kit", "a flashlight", "food", "a phone", "a radio", "water",
              "a bear", "a controller", "a gameboy", "a hat", "a rubix cube", "a soccer ball"};
            //itemDesc = new string[15]{ "to power devices", "to keep someone warm", "for light", "for first aid", "for light", "to eat", "for emergency calls", "to listen to the news", "to drink",
            //    "to play with", "to play with", "to play with", "to play with", "to play with", "to play with"};
            */
            actual = new string[15];
            actualDesc = new string[15];
            ran = new Random();
            curQuestion = "";
            battery = 0;
            blanket = 0;
            candle = 0;
            firstaid = 0;
            flashlight = 0;
            food = 0;
            phone = 0;
            radio = 0;
            water = 0;
            //selected = new bool[15] { false, false, false, false, false, false, false, false, false, false, false, false, false, false, false };
            //done = new bool[9] { false, false, false, false, false, false, false, false, false };


            //randomizePosition();
             
        }
        private void randomizePosition()
        {
            for (int i = 0; i < 15; i++)
            {
                do
                {
                    number = ran.Next(allItems.Count);
                } while (items[number] != null);
                items[number] = content.Load<Texture2D>("Game1/" + itemNames[i]);
                actual[number] = itemsName[i];
                //actualDesc[number] = itemDesc[i];
                if (i < 9)
                    switch (i)
                    {
                        case 0: battery = number; break;
                        case 1: blanket = number; break;
                        case 2: candle = number; break;
                        case 3: firstaid = number; break;
                        case 4: flashlight = number; break;
                        case 5: food = number; break;
                        case 6: phone = number; break;
                        case 7: radio = number; break;
                        case 8: water = number; break;
                    }
            }
        }

        public void LoadContent()
        {
            getItems();
            loadItems();

            bg = content.Load<Texture2D>("Game1/game.png");
            textBox = content.Load<Texture2D>("Game1/storyBox.png");
            continueButton = content.Load<Texture2D>("UI elements/continue.png");
            nextRan();
        }

        private void getItems()
        {
            allItems = itemctr.getNeededItems("is", "needed");
            allItems.AddRange(itemctr.getNeededItems("is", "not needed"));

            foreach (Ontology it in allItems)
            {
                allAssets.Add(itemctr.getItem(it.concept1+"1"));
                isFinished.Add(false);
            }
        }

        private void loadItems()
        {
            foreach (Item it in allAssets)
            {
                allTextures.Add(content.Load<Texture2D>(it.pic));
                allRectangles.Add(new Rectangle(it.x, it.y, it.dimX, it.dimY));
            }
        }

        int ctr = 0;
        public void Update()
        {
            if (ctr != 1000)
            {
                mousePast = mouse;
                // TODO: Add your update logic here
                mouse = Mouse.GetState();
                mouseBox.X = mouse.X;
                mouseBox.Y = mouse.Y;
                checkBoxClick();
                if (!changed)
                {
                    if (!wrong)
                    {
                        convo2 = idesc.getItemDescription2(allItems[random].concept1, "Please help me find something <usedFor>");
                        changed = true;
                        curQuestion = convo2.ElementAt(0).dialogue;
                    }
                    else curQuestion = convo2.ElementAt(0).dialogue + "\n" + convo.ElementAt(0).dialogue;
                }
                ctr++;
            }
            else
            {
                alexGame.inMinigame1 = false;
                alexGame.drawAlexWorld = true;

            }

        }
        private void checkBoxClick()
        {
            
            if (mouse.LeftButton == ButtonState.Pressed && mousePast.LeftButton == ButtonState.Released)
            {
                if (mouseBox.Intersects(continueBox))
                {
                    directions = true;
                }
                else
                {
                    for (int checkCtr = 0; checkCtr < allItems.Count; checkCtr++)
                    {
                        if (mouseBox.Intersects(allRectangles[checkCtr]))
                        {
                            if (checkCorrect(checkCtr) == true)
                                rightChoice(checkCtr);
                            else
                                wrongChoice(checkCtr);
                        }
                    }
                }
            }
        }

        private void rightChoice(int num)
        {
            isFinished[num] = true;
            moveCnt++;
            convoDesc = idesc.getItemDescription2(allItems[random].concept1, "<object> is <usedFor>.");
            curDescription = convoDesc.ElementAt(0).dialogue;
            nextRan();
            changed = false;
            wrong = false;
        }

        private void wrongChoice(int num)
        {
            wrong = true;
            convo2 = idesc.getItemDescription2(allItems[num].concept1, "No. That's something <usedFor>. ");
            convo = idesc.getItemDescription2(allItems[random].concept1, "Help me find something <usedFor>.");
            curQuestion = convo2.ElementAt(0).dialogue + "\n" + convo.ElementAt(0).dialogue;
        }

        private void nextRan()
        {
            do
                random = ran.Next(allItems.Count);
            while (isFinished[random]);
        }

        private bool checkCorrect(int i)
        {
            if (random == i)
            {
                isFinished[random] = true;
                return true;
            }
            else return false;
        }
        public void LoadPeer(String gender)
        {

            peer = content.Load<Texture2D>("Peer/" + gender + "/peer.png");
        }

        public void Draw(SpriteBatch _spriteBatch)
        {
            _spriteBatch.GraphicsDevice.Clear(Color.BlueViolet);
            int i = 0;
            _spriteBatch.Draw(bg, new Vector2(0, 0), Color.White);
            _spriteBatch.Draw(textBox, new Vector2(255, 520), Color.White);
            _spriteBatch.Draw(peer, new Vector2(15, 500), Color.White);

            if (directions == false)
            {
                _spriteBatch.DrawString(content.Load<SpriteFont>("Game3/Fonty"), "Let's help pack!\nFind and click on the item that is being looked for!", new Vector2(325, 555), Color.Black);
                _spriteBatch.Draw(continueButton, new Vector2(975, 700), Color.White);
            }
            else if (moveCnt != 9)
            {

                if (moveCnt > 0 && !wrong)
                {
                    _spriteBatch.DrawString(content.Load<SpriteFont>("Game1/Fonty"), "Nice! " + curDescription + "\n" + curQuestion, new Vector2(325, 555), Color.Black);
                }
                else
                    _spriteBatch.DrawString(content.Load<SpriteFont>("Game1/Fonty"), curQuestion, new Vector2(325, 555), Color.Black);
            }
            else
                _spriteBatch.DrawString(content.Load<SpriteFont>("Game1/Fonty"), "Thank you for helping me out! We need to go now!", new Vector2(325, 555), Color.Black);

            // TODO: Add your drawing code here

            do
            {
                _spriteBatch.Draw(allTextures[i], allRectangles[i], Color.White);
                i++;
            } while (i < 15);

        }

    }
}
